﻿Imports Microsoft.VisualBasic.ApplicationServices

Public Class Form1



    ' Some constants for the Names and Values of the Windows Registry
    Private Const theKeyName As String = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
    Private Const theNameValueName As String = "RegisteredOwner"
    Private Const theOrganizationValueName As String = "RegisteredOrganization"

    ' A little bit of API to add the serious Windows Shield
    Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As UInt32, ByVal sparam As UInt32, ByVal lparam As UInt32) As UInt32
    Private Const BCM_SETSHIELD As Int32 = &H160C

    ' to store the current value
    Private curRegisteredUserName As String
    Private curRegisteredOrganization As String

    Private Shared Sub AddShield(ByVal button As Button)
        button.FlatStyle = FlatStyle.System
        Dim success As UInt32 = SendMessage(button.Handle, BCM_SETSHIELD, 0, 1)
    End Sub

    Private Function IsAdmin() As Boolean
        Return My.User.IsInRole(BuiltInRole.Administrator)
    End Function

    Private Sub ShowWindowsVersionButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowWindowsVersionButton.Click
        ' Run winver to show the windows Dialog.
        Try
            Process.Start("winver")
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
    End Sub

    ' Reads Name and Organization from Registry to the TextBoxes
    Public Sub ReadRegisteredData()
        curRegisteredUserName = My.Computer.Registry.GetValue(theKeyName, theNameValueName, "N/A")
        curRegisteredOrganization = My.Computer.Registry.GetValue(theKeyName, theOrganizationValueName, "N/A")
        Me.RegisteredOwnerTextBox.Text = curRegisteredUserName
        Me.RegisteredOrganisationTextBox.Text = curRegisteredOrganization
    End Sub

    ' Writes Name and Organization Strings of the TextBoxes to the Windows Registry
    Private Sub WriteRegisteredData()
        My.Computer.Registry.SetValue(theKeyName, theNameValueName, Me.RegisteredOwnerTextBox.Text)
        My.Computer.Registry.SetValue(theKeyName, theOrganizationValueName, Me.RegisteredOrganisationTextBox.Text)
    End Sub

    ' Restart this Application as administrator
    Private Sub RestartApplicationAsAdmin()

        Dim aStartInfo As New ProcessStartInfo()
        aStartInfo.Verb = "runas"
        aStartInfo.FileName = My.Application.Info.AssemblyName
        Try
            Process.Start(aStartInfo)
            Me.Close()

        Catch w32Ex As System.ComponentModel.Win32Exception
            If w32Ex.NativeErrorCode = 1223 Then
                ' User said No! - Do Nothing, it's his choice.
            Else
                MsgBox(w32Ex.ToString())
            End If
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try

    End Sub

    ' Update the Title of the Window
    Private Sub UpdateFormTitle()
        If curRegisteredUserName <> Me.RegisteredOwnerTextBox.Text OrElse RegisteredOrganisationTextBox.Text <> curRegisteredOrganization Then
            Me.Text = My.Application.Info.ProductName & "*"
        Else
            Me.Text = My.Application.Info.ProductName
        End If
    End Sub

    Public Sub New()

        ' Dieser Aufruf ist für den Windows Form-Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        Me.ReadRegisteredData()
        If Not Me.IsAdmin() Then
            AddShield(Me.SaveButton)
            Me.SaveButton.Text = "Run As Administrator to Enable Edit"
        Else
            Me.RegisteredOwnerTextBox.ReadOnly = False
            Me.RegisteredOrganisationTextBox.ReadOnly = False
            Me.SaveButton.Text = "Save"
            Me.SaveButton.Image = My.Resources.document_save_32x32
        End If
    End Sub

    Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click

        ' If the User isn't the admin try to restart this application 
        ' as admin and close this application and exit this sub.
        If Not Me.IsAdmin() Then
            RestartApplicationAsAdmin()
            Exit Sub
        End If

        ' Write Edited Data back to the Registry
        Me.WriteRegisteredData()
        Me.ReadRegisteredData()
        Me.UpdateFormTitle()
    End Sub

    Private Sub Text_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegisteredOwnerTextBox.TextChanged, RegisteredOrganisationTextBox.TextChanged
        Me.UpdateFormTitle()
    End Sub


End Class

